/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.economy.web.action.job;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ar.uba.fi.posgrado.common.model.User;
import ar.uba.fi.posgrado.common.service.UserService;
import ar.uba.fi.posgrado.economy.service.BenefitService;
import ar.uba.fi.posgrado.economy.service.DebtService;
import ar.uba.fi.posgrado.economy.service.HonoraryService;
import ar.uba.fi.posgrado.economy.web.form.job.JobForm;

/**
 * Action que ejecuta servicios online, que corren generalmente en forma batch, de manera
 * de poder correr algo de manera de ser totalmente necesarios.<br><br>
 * 
 * <b>Solo</b> los administradores pueden acceder a este recurso.<br>
 * 
 * @author nicolas.gonzalez
 * @see Action
 */
public class ExecuteJobOnline extends Action {
	/* El mapeo del nombre del job contra el job en cuestion
	 * esta hardcodeado en esta clase y se recibe por parametro
	 * desde la vista.
	 * En caso de modificar estos valores, MANTENER coherencia con listJobs.jsp
	 */
	
	/** Logger */
	private static Logger logger = Logger.getLogger(ExecuteJobOnline.class);
	
	private DebtService debtService;
	private BenefitService benefitService;
	private HonoraryService honoraryService;
	private UserService userService;
	
	// IoC
	public void setDebtService(DebtService debtService) {
		this.debtService = debtService;
	}
	// IoC
	public void setBenefitService(BenefitService benefitService) {
		this.benefitService = benefitService;
	}
	// IoC
	public void setHonoraryService(HonoraryService honoraryService) {
		this.honoraryService = honoraryService;
	}
	// IoC
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	

    /**
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("Por ejecutar un job en forma online ...");
        }
        JobForm form = (JobForm)actionForm;

        Principal ppal =  request.getUserPrincipal();
        String userName = ppal.getName();
        User user = this.userService.getUserByName(userName);
        
        String jobName = form.getJobName();
        if (jobName.equalsIgnoreCase("debt")) {
        	logger.info("Por ejecutar el servicio de activacion/desactivacion de " +
        				"Deudas en forma online a pedido del administrador: " +
        				user);
        	this.debtService.executeOnlineJobs(user);
        	form.setResponseDescription("Activacion y desactivacion de deudas ejecutado satisfactoriamente");
        }
        else if (jobName.equalsIgnoreCase("benefit")) {
        	logger.info("Por ejecutar el servicio de activacion/desactivacion de " +
    				"Beneficios en forma online a pedido del administrador: " +
    				user);
			this.benefitService.executeOnlineJobs(user);
        	form.setResponseDescription("Activacion y desactivacion de beneficios ejecutado satisfactoriamente");
		}
        else if (jobName.equalsIgnoreCase("honorary")) {
        	logger.info("Por ejecutar el servicio de activacion de " +
    					"Honorarios en forma online a pedido del administrador: " +
    					user);
			this.honoraryService.executeOnlineJobs(user);
        	form.setResponseDescription("Activacion de honorarios ejecutado satisfactoriamente");
		}
        
        logger.info("Ejecucion del job seleccionado exitosamente ...");
        return mapping.findForward("success");
    }
	
}

